<?php
namespace App\Models\OrderModels;
use App\Models\ParentModel;
use DB;
/**
 * 合伙人用户model
 * @author puppet
 */
class Order extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_order';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_order';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
	/**
	*	$where  条件	如：id=1 and ky=2	
	*	$data	数据集
	*/
	public function updateOrder($where,$data){
		if(isset($data) && !empty($data)){
			$sql="update $this->table set ";
			foreach($data as $k=>$v){
				$sql.="`".$k."`='".$v."',";
			}
			$sql=substr($sql,0,-1)." where ".$where;
			// echo $sql;exit();
			DB::update($sql);
		}
	}
	
	/**
	*	订单数据列表
	*/
	public function getPageData($params){
		// echo "<pre>";print_r($params);exit();
		$res = false;
		$limit_num = (isset($params['limit']) && intval($params['limit']) > 0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$limit_sta = 0;
		$page = 1;
		$search_order_no=isset($params['search_order_no']) ? $params['search_order_no'] : '';
		$search_type=isset($params['search_type']) ? $params['search_type'] : '';
		$search_pay_type=isset($params['search_pay_type']) ? $params['search_pay_type'] : '';
		$search_userName=isset($params['search_userName']) ? $params['search_userName'] : '';
		
		if (isset($params['page']) && intval($params['page']) > 0) {
			$limit_sta = intval($limit_num * intval($params['page'] - 1));
			$page = intval($params['page']);
		}
		$values=array(0);
		$countSql="select count(*) as count from $this->table as a left join pt_partner as b on b.id=a.partner_id where a.partner_id > ?";
		$sql="select a.id,a.order_no,a.type,a.pay_type,a.price,a.discount,a.status,a.title,a.create_time,b.user_name from $this->table as a";//
		$sql.=" left join pt_partner as b on b.id=a.partner_id where a.partner_id > ?";
		
		$role_id=session('user')[0]['role_id'];
		if($role_id!=1){
			$countSql.=" and b.fid = ?";
			$sql.=" and b.fid = ?";
			$values[]=$role_id;
		}
		if($search_order_no){
			$countSql.=" and a.order_no like '%{$search_order_no}%'";
			$sql.=" and a.order_no like '%{$search_order_no}%'";
		}
		if($search_type){
			$countSql.=" and a.type = ?";
			$sql.=" and a.type = ?";
			$values[]=$search_type;
		}
		if($search_pay_type){
			$countSql.=" and a.pay_type = ?";
			$sql.=" and a.pay_type = ?";
			$values[]=$search_pay_type;
		}
		if($search_userName){
			$countSql.=" and b.user_name like '%{$search_userName}%'";
			$sql.=" and b.user_name like '%{$search_userName}%'";
		}
		// echo $countSql;exit();
		 //echo "<pre>";print_r($sql);exit();
		$count = DB::select($countSql,$values);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$sql.= " order by a.create_time desc LIMIT $limit_sta,$limit_num";

		$data = DB::select($sql,$values);
		$config=Config('basics');
		if(is_array($data)){
			foreach($data as $k=>$v){
				$data[$k]['type']=$config['type'][$v['type']];
				$data[$k]['status']=$config['pay_status'][$v['status']];
				$data[$k]['pay_type']=$config['pay_type'][$v['pay_type']];
			}
		}
		// echo $data[0]['pay_type'];
		// echo "<pre>";print_r($data);exit();
		
		return array('data'=>$data,'total'=>$count);
	}
	
	/**
	*	查询单个订单详情 订单列表
	*/
	public function orderOne($data){
		$i=0;$values=array();$resout=array();
		if(is_array($data)){
			$sql="select a.id,a.order_no,a.type,a.pay_type,a.price,a.discount,a.status,a.title,a.create_time,b.user_name from $this->table as a";
			$sql.=" left join pt_partner as b on b.id=a.partner_id where";
			foreach($data as $k=>$v){
				$values[]=$v;
				if($i==0){
					$sql.=" a.$k=?";
				}else{
					$sql.=" and a.$k=?";
				}
				$i++;
			}
			$sql.=" limit 1";
			// echo $sql;
			// var_dump($values);exit();
			$list=DB::select($sql,$values);
			if(!empty($list)){
				$resout=$list[0];
			}
		}
		return $resout;
	}
	
	
	//金额变动记录拉取
	function getPageDataList($params){
		$search_pname=isset($params['search_pname']) ? $params['search_pname'] : false;
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;


        $limit_sta = 0;
		$page = 1;
        if (isset($params['page']) && intval($params['page']) > 0) {
            $limit_sta = intval($size * intval($params['page'] - 1));
            $page = intval($params['page']);
        }
		$countSql="select count(a.id) as count from pt_price_change as a left join pt_partner as b on b.id=a.partner_id where 1=?";
		$sql="select a.*,b.user_name from pt_price_change as a left join pt_partner as b on b.id=a.partner_id where 1=?";
		
		if($search_pname){
			$countSql.=" and b.user_name like '%{$search_pname}%'";
			$sql.=" and b.user_name like '%{$search_pname}%'";
		}
		$sql.=" order by create_time desc limit $limit_sta,$size";
		$count=DB::select($countSql,[1]);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$data=DB::select($sql,[1]);
		
		if($data){
			foreach($data as $k=>$v){
				$data[$k]['price_type']=!$v['price_type'] ? "收入" : ($v['price_type']==1 ? "支出" : "平台修改");
				if($v['create_type']){
					$inSql="select user_name,type from pt_partner where id=?";
					$rl=DB::select($inSql,[$v['create_id']]);
					$data[$k]['name']=isset($rl[0]['user_name']) ? $rl[0]['user_name'] : '';
					$data[$k]['create_type']=isset($rl[0]['type']) && $rl[0]['type']==1 ? "合伙人" : "会员";
				}else{
					$inSql="select admin_name from pt_admin where admin_id=?";
					$rl=DB::select($inSql,[$v['create_id']]);
					$data[$k]['name']=isset($rl[0]['admin_name']) ? $rl[0]['admin_name'] : '';
					$data[$k]['create_type']="后台管理员";
				}
			}
		}
		
		return array('data'=>$data,'total'=>$count);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}